﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace ALDIOrganizer
{
    [Serializable]
    public class PopupMenuEntry
    {
        internal ToolStripMenuItem item;

        public delegate bool QualifierDelegate(Cell pCell);
        internal bool bQualifies;

        internal QualifierDelegate dQualifies;
        internal delegate List<ToolStripMenuItem> delPopulates();        
        internal PopupMenuEventHandlers.PopulatorDelegate dPopulates;
        internal List<ToolStripMenuItem> listPrepopulated;

        internal bool separatorAfter;

        public PopupMenuEntry(ToolStripMenuItem item, bool separatorFollows)
        {
            this.item = item;
            this.separatorAfter = separatorFollows;
        }
  
        public PopupMenuEntry(ToolStripMenuItem item, QualifierDelegate qualifies, bool separatorFollows) : this(item,separatorFollows)
        {            
            this.dQualifies = qualifies;         
        }

        public PopupMenuEntry(ToolStripMenuItem item, bool qualifies, bool separatorFollows) : this(item, separatorFollows)
        {            
            this.bQualifies = qualifies;         
        }

        public PopupMenuEntry(PopupMenuEventHandlers.PopulatorDelegate populates, bool qualifies, bool separatorFollows)
        {
            this.bQualifies = qualifies;
            this.dPopulates = populates;
            this.separatorAfter = separatorFollows;
        }

        public PopupMenuEntry(PopupMenuEventHandlers.PopulatorDelegate pPopulates, QualifierDelegate qualifies, bool separatorFollows)
        {
            this.dQualifies = qualifies;
            this.dPopulates = pPopulates;
            this.separatorAfter = separatorFollows;
        }

        public PopupMenuEntry(ToolStripMenuItem parrentItem, PopupMenuEventHandlers.PopulatorDelegate populates, bool qualifies, bool separatorFollows) : this(parrentItem,separatorFollows)
        {
            this.bQualifies = qualifies;
            this.dPopulates = populates;            
        }

        public PopupMenuEntry(ToolStripMenuItem parentItem, PopupMenuEventHandlers.PopulatorDelegate populates, QualifierDelegate qualifies, bool separatorFollows)
            : this(parentItem, separatorFollows)
        {
            this.dQualifies = qualifies;
            this.dPopulates = populates;
        }

        public PopupMenuEntry(List<ToolStripMenuItem> prepopulated, bool qualifies, bool separatorFollows)
        {
            this.bQualifies = qualifies;
            this.listPrepopulated = prepopulated;
            this.separatorAfter = separatorFollows;
        }

        public PopupMenuEntry(ToolStripMenuItem parrentItem, List<ToolStripMenuItem> prepopulated, bool qualifies, bool separatorFollows)
            : this(parrentItem, separatorFollows)
        {
            this.bQualifies = qualifies;
            this.listPrepopulated = prepopulated;
        }

        /// <summary>
        /// CellMouseDown event handler for DataGridView - provides context menu for the control.
        /// </summary>        
        public static void ShowContextMenu(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (sender.GetType() != typeof(SheetView))
            {                
                return;
            }
            var _dgrid = (SheetView)sender;
            if (e.Button != MouseButtons.Right)
            {
                return;
            }
            var _row = e.RowIndex;
            if (_row == -1)
            {
                return;
            }
            var _column = e.ColumnIndex;
            var cell = _dgrid.Underlying(_column, _row);

            if (_dgrid.refill is Rota)
            {
                _dgrid.PopupLayout.Bind(
                    _dgrid.refillRota,
                    _dgrid,
                    cell,
                    _row);
            }
            else if (_dgrid.refill is Template)
            {
                _dgrid.PopupLayout.Bind(
                    _dgrid.refill_template,
                    _dgrid,
                    cell,
                    _row);
            }
            
            var menu = PopupMenuLayout.Build(
                _dgrid.PopupLayout.GetLayout(cell),
                cell);
            _dgrid[_column, _row].ContextMenuStrip = menu;
            _dgrid.CurrentCell = _dgrid[_column, _row];
        }
        
        

    }
}
